home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_09_07
/
9n07123a
< prev
next >
Wrap
Text File
|
1991-05-21
|
1KB
|
71 lines
Listing 9
/*
* xrt.cpp - cross-reference table
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ln_seq.h"
#include "xrt.h"
class treenode
{
public:
char *word;
ln_seq lines;
xrt left, right;
treenode(unsigned n);
~treenode();
};
treenode::treenode(unsigned n) : lines(n) { }
treenode::~treenode()
{
delete word;
// delete left;
// delete right;
}
xrt::xrt() : root(0) { }
xrt::~xrt()
{
delete root;
}
void xrt::add(char *w, unsigned n)
{
int cond;
if (root == 0)
{
root = new treenode(n);
root->word = new char[strlen(w) + 1];
strcpy(root->word, w);
// root->left = root->right = 0;
}
else if ((cond = strcmp(w, root->word)) == 0)
root->lines.add(n);
else if (cond < 0)
root->left.add(w, n);
else
root->right.add(w, n);
}
void xrt::print()
{
if (root != 0)
{
root->left.print();
printf("%12s: ", root->word);
root->lines.print();
printf("\n");
root->right.print();
}
}